const testEmptyArray=(arr)=>arr==undefined?[]:arr;

String.prototype.contains = function (str) {
    return this.indexOf(str) != -1;
};

String.prototype.escapeText = function () {
    return this.replace(/"/g,'\\"').replace(/\n/g,'\\\\n');
};

String.prototype.unescapeText = function () {
    return this.unescapeEnter().unescapeQuota();
};

String.prototype.escapeQuota = function () {
    return this.replace(/"/g,'\\"');
};

String.prototype.unescapeQuota = function () {
    return this.replace(/\\"/g,'"');
};

String.prototype.escapeEnter = function () {
    return this.replace(/\n/g,'\\\\n');
};

String.prototype.unescapeEnter = function () {
    return this.replace(/\\\\n/g,'\n');
};

function getRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = {};
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

function getConfigure(ftype,callback) {
    if(ftype == null){
        return;
    }

    let session = neo4jService.getSession();
    let resultPromise = session.run(`MATCH (n:Configure) WHERE n.ftype="${ftype}" RETURN n`);
    resultPromise.then(result => {
        session.close();
        let confs = result.records.map((record) => record.get(0));
        let rs = [];
        if(confs.length>0){
            let conf = confs[0];
            let data = conf['properties']['data'];
            rs = JSON.parse(data);
            // rs = data.map(x=>x.name);
        }
        callback(rs);
    });
}

const cache={};
//异步缓存函数
function getCache(name,callback,supplier,timeout=5000) {
    let now = new Date().getTime();
    if(cache[name] && (now - cache[name].lastTimestamp<=timeout)){
        callback(cache[name].value);
    }else {
        supplier(callback,cache,name);
    }
}
//同步的缓存
function getCacheSync(name,supplier,timeout=5000) {
    let now = new Date().getTime();
    if(cache[name] && (now - cache[name].lastTimestamp<=timeout)){
        return cache[name].value;
    }else {
        let val = supplier();
        cache[name]={
            lastTimestamp:now,
            value:val
        };
        return val;
    }
}

window.basePort = localStorage.getItem("serverPort");
window.basePort = window.basePort || 8082;

